package com.pfzy.lib.net.utils;

import com.pfzy.lib.sdk.log.UXLog;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.util.List;

public final class ShellUtils {
    private static final String LINE_SEP = System.getProperty("line.separator");

    private ShellUtils() {
        UXLog.e("ShellUtils","u can't instantiate me...");
    }

    public static CommandResult execCmd(String command, boolean isRooted) {
        return execCmd(new String[]{command}, isRooted, true);
    }

    public static CommandResult execCmd(List<String> commands, boolean isRooted) {
        return execCmd(commands == null ? null : (String[]) commands.toArray(new String[0]), isRooted, true);
    }

    public static CommandResult execCmd(String[] commands, boolean isRooted) {
        return execCmd(commands, isRooted, true);
    }

    public static CommandResult execCmd(String command, boolean isRooted, boolean isNeedResultMsg) {
        return execCmd(new String[]{command}, isRooted, isNeedResultMsg);
    }

    public static CommandResult execCmd(List<String> commands, boolean isRooted, boolean isNeedResultMsg) {
        return execCmd(commands == null ? null : (String[]) commands.toArray(new String[0]), isRooted, isNeedResultMsg);
    }

    public static CommandResult execCmd(String[] arg21, boolean arg22, boolean arg23) {
        int v13 = -1;
        if(arg21 != null && arg21.length != 0) {
            DataOutputStream os = null;
            Process process = null;
            StringBuilder v15 = null;
            BufferedReader v17 = null;
            StringBuilder v6 = null;
            BufferedReader v8 = null;
            String v9 = null;

            try {
                // 执行这一句，superuser.apk就会弹出授权对话框
                process  = Runtime.getRuntime().exec("sh");
                os  = new DataOutputStream(process .getOutputStream());

                for(int v18_1 = 0; v18_1 < arg21.length; ++v18_1) {
                    String v3 = arg21[v18_1];
                    if(v3 != null) {
                        os.write(v3.getBytes());
                        os.writeBytes(ShellUtils.LINE_SEP);
                        os.flush();
                    }
                }

                os.writeBytes("exit" + ShellUtils.LINE_SEP);
                os.flush();
                v13 = process .waitFor();
                if(arg23) {
                    v15 = new StringBuilder();
                    v6 = new StringBuilder();
                    v17 = new BufferedReader(new InputStreamReader(process .getInputStream(), "UTF-8"));
                    v8 = new BufferedReader(new InputStreamReader(process .getErrorStream(), "UTF-8"));

                    while(true) {
                        v9 = v17.readLine();
                        if(v9 == null) {
                            break;
                        }
                        v15.append(ShellUtils.LINE_SEP).append(v9);
                    }

                    while(true) {
                        v9 = v8.readLine();
                        if(v9 == null) {
                            break;
                        }
                        v6.append(ShellUtils.LINE_SEP).append(v9);
                    }
                }

                if(os != null) {
                    os.close();
                }

                if(v17 != null) {
                    v17.close();
                }

                if(v8 != null) {
                    v8.close();
                }

                if(process  != null) {
                    process .destroy();
                }

                String v20 = v15 == null ? "" : v15.toString();
                String v18_2 = v6 == null ? "" : v6.toString();

                return new CommandResult(v13, v20, v18_2);
            }
            catch(Exception v4) {
                v4.printStackTrace();
                return new CommandResult(v13, "", "");
            }
            catch(Throwable v18) {
                throw v18;
            }
        }

        return new CommandResult(v13, "", "");
    }

    public static class CommandResult {
        public String errorMsg;
        public int result;
        public String successMsg;

        public CommandResult(int result, String successMsg, String errorMsg) {
            this.result = result;
            this.successMsg = successMsg;
            this.errorMsg = errorMsg;
        }

        public String toString() {
            return "result: " + this.result + "\nsuccessMsg: " + this.successMsg + "\nerrorMsg: " + this.errorMsg;
        }
    }
}
